<?php
/**
* @version $Id: index.php 9 2006-09-16 15:06:10Z RussW $
* @package Joomla! Tools Suite
*/

// No Direct Access, Redirect To Index
if (!defined( '_VALID_JTS' )) { 
								$host  = $_SERVER['HTTP_HOST'];
								$uri  = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
								header("Location: http://$host$uri/");
								exit;
							  }

// Set flag that this is a parent file
define( '_VALID_MOS', 1 );

	if (@file_exists( '../configuration.php' )) {
		include( '../configuration.php' );
	}

   set_time_limit( 100 );
   $time = microtime();
   $time = explode(' ', $time);
   $time = $time[1] + $time[0];
   $start = $time;
//   $date = `date`;
//   $hostname = `hostname`;
/***********************************
  Connection variables :
***********************************/
// If your have root permissions
   $h = ''.$mosConfig_host.'';
   $u = ''.$mosConfig_user.'';
   $p = ''.$mosConfig_password.'';

   $dummy_db = ''.$mosConfig_db.'';

   $db_link = @mysql_connect($h,$u,$p);
   $res = @mysql_db_query($dummy_db, 'SHOW DATABASES LIKE \''.$mosConfig_db.'\'', $db_link);
   // or die('Could not connect: ' . mysql_error());
?>

	<div id="ctr" align="center">
		<div class="install">
		<div id="stepbar">
			<p>
			<table border='0' width='170px' cellspadding='1' cellspacing='0' align='center'><tr><td  class='advisor' height="23px" valign="middle">
	  		<?php
			echo "<b>". _VERNAME ." ". _DIAG_VERSION ." : ". _LANGUAGE ."</b><br />";
//			echo "<b>". _HISA_TRANSLATEDBY.":</b><br />"._HISA_TRANSLATEDWHO ."<br />";
			?>
			</td></tr></table><br clear=all />
			</p>

			<p>
			<table border='0' width='96%' cellspadding='2' cellspacing='3' align='center' class="content">

			<?php if ($_POST['optConfirm'] != 'y') {
				echo "<tr><td  class='bad-message'>";
			      if (!$db_link) { 
				    echo "". _DB_NAME ." ". _UNAVAIL .""; 
					} else {
				      echo "<b><font color='black'>". _RSPW_CONFIRM_REQ ."</font></b><br />". _RSPW_CONFIRM_REQMSG;
                  }
				//echo "</td></tr>
				echo "<tr><td>&nbsp;</td></tr><td style='background-color: #FFFFFF; border: 1px solid #C0C0C0; padding-top: 8px; padding-bottom: 8px; padding-left: 2px; padding-right: 2px;' align='center'>";
				echo "<form action='". $_SERVER['PHP_SELF'] ."?opt=dbop' method='POST'>";
				echo "<input type='hidden' value='y' name='optConfirm'>";
                if (!$db_link) {
				  echo "<input type='submit' class='confirmno' style='color: #808080;' value='". _DBOP_CONFIRM ."' disabled>";
				  } else {
				    echo "<input type='submit' class='confirmno' value='". _DBOP_CONFIRM ."'>";
				}
				echo "</form>";
				echo "</td></tr>";
			} ?>
			<?php if ($_POST['optConfirm'] != 'y') {
			  echo "<tr><td class='warn-message'><font color='#606060'>". _DBOP_DESC_BEFORE ."</font></td></tr>";
			} else {
			  echo "<tr><td class='good-message'><font color='#606060'>". _DBOP_DESC_AFTER ."</font></td></tr>";
			} ?>
			</table>
			</p>
		</div>

			<div id="right">

  			<div class="clr"></div>
  			<h1><?php echo _DBOP_TITLE; ?></h1>
	  		<div class="install-text">
  				<p><?php echo _DBOP_DESC_MAIN; ?></p>
  			</div>
			<div class="install-form">
  				<div class="form-block">


    <table class="content" cellpadding="0" cellspacing="1" border="0" width="100%">
	<tr>
		<td class="item"><b><?php echo _SQL_HOST; ?></b></td><td class="item"><?php echo $mosConfig_host; ?></td>
	</tr>
    <tr>
	    <td class="item" width="30%"><b><?php echo _SVRVER; ?></b></td>
		<td class="item" width="70%"><?php echo @mysql_get_server_info(); ?></td>
	</tr>
    <tr>
	    <td class="item">&nbsp; - <?php echo _CLIENTVER; ?></td>
		<td class="item"><?php echo @mysql_get_client_info(); ?></td>
	</tr>
    <tr>
	    <td class="item">&nbsp; - <?php echo _HOSTINFO; ?></td>
		<td class="item"><?php echo @mysql_get_host_info(); ?></td>
	</tr>
    <tr>
	    <td class="item">&nbsp; - <?php echo _PROTVER; ?></td>
		<td class="item"><?php echo @mysql_get_proto_info(); ?></td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td align='left'>
	<br>

	<table width="100%" cellpadding="1" cellspacing="0" border="0">
	<?php
	if ($_POST['optConfirm'] == "y") {

// SESSIONS TESTING
    echo "<tr>";
	echo "<td class='Heading' style='border: 1px solid #C0C0C0;'><b>". _DBOP_STALE_SESSIONS ."</b></td>";
	echo "<td class='vflist'>&nbsp;&nbsp;";

   		$stale = time()-$mosConfig_lifetime;
        $sessionDeleteQuery ="SELECT * FROM ". $mosConfig_dbprefix ."session";
        $sessionDeleteResult=mysql_query($sessionDeleteQuery, $db_link);

        while ($row = mysql_fetch_array($sessionDeleteResult)) {
          if ($row['time'] < $stale) {
            $deleteSession ="DELETE FROM ". $mosConfig_dbprefix ."session WHERE time < '$stale'";
            $deleteQuery=mysql_query($deleteSession, $db_link);
          }
        }
        echo "<font color='green'>". _DBOP_CLEANED ."</font>";
	    echo "</td>";
    	echo "</tr>";
        echo "<tr><td>&nbsp;</td><td>&nbsp;</td></tr>";
    ?>
	<tr>
		<td class="Heading" style="border-left: 1px solid #C0C0C0; border-top: 1px solid #C0C0C0; border-bottom: 1px solid #C0C0C0;" width="45%"> &nbsp;<b><?php echo _DB_NAME; ?></b></td>
		<td class="Heading" width="55%" style="border: 1px solid #C0C0C0;"> &nbsp;<b><?php echo _DB_STATUS; ?></b></td>
	</tr>

<?php
   	$dbs = array();

	while ( $rec = mysql_fetch_array($res) )
   	{
    	$dbs [] = $rec [0];
   	}

    foreach ( $dbs as $db_name )
    {
		echo "<tr><td class='vflist'>&nbsp;" . $db_name ."&nbsp;&nbsp;</td>";

		$res = mysql_db_query($dummy_db, "SHOW TABLE STATUS FROM `" . $db_name . "`", $db_link) or die('Query : ' . mysql_error());
		$to_optimize = array();

        while ( $rec = mysql_fetch_array($res) )
        {
          if ( $rec['Data_free'] > 0 )
          {
            $to_optimize [] = $rec['Name'];
          }
        }

	if ( count ( $to_optimize ) < 1 )
	{
		echo "<td class='vflist'>&nbsp;&nbsp;<i><font color='blue'>"._DBOP_NOTREQD."</font></i></td></tr>";
    }

    if ( count ( $to_optimize ) > 0 )
	{
    	echo "<td class='vflist'>&nbsp;&nbsp;<i><font color='red'>"._DBOP_REQD."</font></i></td></tr>";

           foreach ( $to_optimize as $tbl )
           {
				mysql_db_query($db_name, "OPTIMIZE TABLE `" . $tbl ."`", $db_link );
				
				echo "<tr><td class='vflist' colspan='2' align='right'>". $tbl ." <font color='green'><i>"._DBOP_OPTIMISED."</i></font></td></tr>";
           }
       }
    }

    $time = microtime();
    $time = explode(' ', $time);
    $time = $time[1] + $time[0];
    $finish = $time;
    $total_time = round(($finish - $start), 6);
	?>

	<tr>
		<td class="Heading" colspan="2" style="border: 1px solid #c0c0c0;"><?php echo "".$mosConfig_db ." "._DBOP_TIME."<br />". $total_time ." "._SECONDS.""; ?></td>
	</tr>

     <?php } // optConfirm = y ?>
	</table>

	</td>
  </tr>
  </table>

  				</div>
			</div>

		</div>
		<div class="clr"></div>
	</div>	




	<div id="ctr" align="center">
		<div class="install">
		<div id="stepbar">
			<div onClick="expandcontent('dbtableDetail')" style="cursor:hand; cursor:pointer">
			<img src="images/db.png" width="16" height="16" alt="">&nbsp;Show/Hide
			</div>
		</div>

			<div id="right">	


  			<div class="clr"></div>
  			<h1><?php echo _JOOMLA ." ". _DBOP_TABLECHK; ?>:</h1>
            <div id="dbtableDetail" class="switchcontent">
		  		<div class="install-text">
	  				<p><?php echo _DBOP_TABLECHK_DESC; ?></p>
	  			</div>
			<div class="install-form">
  				<div class="form-block">
			<?php

	        if (!$db_link) { // Cannot Connect To DB
				echo "<table border='0' width='96%' cellspadding='2' cellspacing='3' align='center'>";
				echo "<tr><td  class='bad-message'>";
		   		echo "". _DB_NAME ." ". _UNAVAIL .""; 
				echo "</td></tr>";
				echo "</table>";
			  	} else {


			$db = @mysql_select_db("". $mosConfig_db ."", $db_link)
			  or die("Couldn't select the database.");

			$tablenames = array(
								''. $mosConfig_dbprefix .'banner',
								''. $mosConfig_dbprefix .'bannerclient',
								''. $mosConfig_dbprefix .'bannerfinish',
								''. $mosConfig_dbprefix .'categories',
								''. $mosConfig_dbprefix .'components',
								''. $mosConfig_dbprefix .'contact_details',
								''. $mosConfig_dbprefix .'content',
								''. $mosConfig_dbprefix .'content_frontpage',
								''. $mosConfig_dbprefix .'content_rating',
								''. $mosConfig_dbprefix .'core_acl_aro',
								''. $mosConfig_dbprefix .'core_acl_aro_groups',
								''. $mosConfig_dbprefix .'core_acl_aro_sections',
								''. $mosConfig_dbprefix .'core_acl_groups_aro_map',
								''. $mosConfig_dbprefix .'core_log_items',
								''. $mosConfig_dbprefix .'core_log_searches',
								''. $mosConfig_dbprefix .'groups',
								''. $mosConfig_dbprefix .'mambots',
								''. $mosConfig_dbprefix .'menu',
								''. $mosConfig_dbprefix .'messages',
								''. $mosConfig_dbprefix .'messages_cfg',
								''. $mosConfig_dbprefix .'modules',
								''. $mosConfig_dbprefix .'modules_menu',
								''. $mosConfig_dbprefix .'newsfeeds',
								''. $mosConfig_dbprefix .'poll_data',
								''. $mosConfig_dbprefix .'poll_date',
								''. $mosConfig_dbprefix .'poll_menu',
								''. $mosConfig_dbprefix .'polls',
								''. $mosConfig_dbprefix .'sections',
								''. $mosConfig_dbprefix .'session',
								''. $mosConfig_dbprefix .'stats_agents',
								''. $mosConfig_dbprefix .'template_positions',
								''. $mosConfig_dbprefix .'templates_menu',
								''. $mosConfig_dbprefix .'users',
								''. $mosConfig_dbprefix .'usertypes',
								''. $mosConfig_dbprefix .'weblinks'
								);
				?>


				<table class="content" border="0" cellpadding="0" cellspacing="1">
				<tr>
				  <th class="vflist" align="left"> &nbsp;<?php echo _DBOP_TABLENAME; ?></th>
				  <th class="vflist" align="left"> &nbsp;<?php echo _DBOP_TABLESTATUS; ?></th>
				  <td></td>
				</tr>
				<?php
				 foreach($tablenames as $table){
					$result = mysql_query("SHOW TABLES LIKE '". $table ."'");
						$tablecheck = "CHECK TABLE ". $table ."";
						$chkresult = mysql_query($tablecheck);
						$check = (mysql_num_rows($chkresult));

							if(!$check == 'OK') {// table needs repair
								$chkMsg = "&nbsp;( <font color='red'>". _DBOP_TABLEREPAIR ."</font> )";
							} else { 
								$chkMsg = "&nbsp;( <font color='green'>". _DBOP_TABLEOK ."</font> )"; 
							}

					echo (mysql_num_rows($result)) ? '<tr><td class="small-good" style="font-size: 11px;"> &nbsp;&nbsp;'. $table .' </td><td> &nbsp;&nbsp;'. _DBOP_TABLEPRESENT .'&nbsp;'. $chkMsg .'</td></tr>' . "\n" : '<tr><td class="small-bad" style="color: red;">  &nbsp;&nbsp;'. $table .'</td><td style="color: red; background-image: url(../includes/js/ThemeOffice/warning.png); background-position: 5px 0px; background-repeat: no-repeat; padding-left: 18px;"> &nbsp;'. _DBOP_TABLEMISSING .'</td></tr>';

                 }
				?>
                </table>
		<?php } // Cannot Connect to DB ?>

                  </div>
  				</div>
			</div>

		</div>
		<div class="clr"></div>
	</div>		
	



	<div id="ctr" align="center">
		<div class="install">
		<div id="stepbar">
			<div onClick="expandcontent('dbsessionDetail')" style="cursor:hand; cursor:pointer">
			<img src="images/db.png" width="16" height="16" alt="">&nbsp;Show/Hide
			</div>
		</div>

			<div id="right">	


  			<div class="clr"></div>
  			<h1><?php echo _JOOMLA ." ". _DBOP_SESSION; ?>:</h1>
             <div id="dbsessionDetail" class="switchcontent">
		  		<div class="install-text">
  				<p>Session Life is <?php echo $mosConfig_lifetime ." ". _SECONDS; ?></p>
  				</div>
				<div class="install-form">
			<div class="form-block">
			<?php

	        if (!$db_link) { // Cannot Connect To DB
				echo "<table border='0' width='96%' cellspadding='2' cellspacing='3' align='center'>";
				echo "<tr><td  class='bad-message'>";
		   		echo "". _DB_NAME ." ". _UNAVAIL .""; 
				echo "</td></tr>";
				echo "</table>";
			  	} else {

				$db = @mysql_select_db("". $mosConfig_db ."", $db_link)
				  or die("Couldn't select the database.");

	            $sessionQuery ="SELECT * FROM ". $mosConfig_dbprefix ."session";
	            $sessionResult=mysql_query($sessionQuery, $db_link);
	   		    $stale = time()-$mosConfig_lifetime;
			?>

             <table class="content" border="0" cellpadding="0" cellspacing="1">
             <tr>
			   <th class="vflist" align="left">&nbsp;<?php echo _USERNAME; ?></th>
			   <th class="vflist" align="left">&nbsp;<?php echo _USERTYPE; ?></th>
			   <th class="vflist">&nbsp;<?php echo _STALE; ?>&nbsp;</th>
			 </tr>

			<?php
            	while ($row = mysql_fetch_array($sessionResult)) {

	                if ($row['time'] < $stale) {
	                  $isStaleHi = "blue";
					  $isStale   = _YES;
    	            } else {
	                  $isStaleHi = "green";
					  $isStale   = _NO;
					}
			?>
            <tr>
			  <td>&nbsp;&nbsp;<font color="<?php echo $isStaleHi; ?>"><?php echo $row['username']; ?></font></td>
			  <td>&nbsp;&nbsp;<?php echo $row['usertype']; ?></td>
			  <td>&nbsp;&nbsp;<font color="<?php echo $isStaleHi; ?>"><?php echo $isStale; ?></font></td>
			</tr>
      <?php } ?>
			</table>
    <?php } // Cannot Connect to DB ?>

                  </div>
  				</div>
			</div>

		</div>
		<div class="clr"></div>
	</div>		




	<div class="error" style="width: 755px; padding: 2px; font-size: 11px; font-weight: normal; background-color: yellow; text-align: center; background-image: url(../includes/js/ThemeOffice/warning.png); background-position: 4px 4px; background-repeat: no-repeat; border: 1px solid #cc0000;">
	<?php echo _WARN_SECURITY; ?>
	</div>
		<br /><br />
		<?php echo _JUST_LINK; ?><br />
	<br />
	<div class="ctr">
	<?php echo _JOOMLA_LINK; ?>
	</div>

	</body>
	</html>
	
	
